package com.ms.education.course.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ms.education.course.entity.*;
import com.ms.education.course.mapper.PushScheduleMapper;
import com.ms.education.course.service.PushClassService;
import com.ms.education.course.service.PushCourseService;
import com.ms.education.course.service.PushScheduleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 推送计划 服务实现类
 * </p>
 *
 * @author ms
 * @since 2023-03-06
 */
@Service
public class PushScheduleServiceImp extends ServiceImpl<PushScheduleMapper, PushSchedule> implements PushScheduleService {

    @Autowired
    private PushClassService pushClassService;

    @Autowired
    private PushCourseService pushCourseService;

    @Override
    public Page<PushSchedule> pagePush(long current, long limit, PushSchedule pushSchedule) {
        Page<PushSchedule> page = new Page<>(current, limit);
        String title = pushSchedule.getTitle();
        Date pushDate = pushSchedule.getPushDate();
        QueryWrapper<PushSchedule> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(title)) {
            wrapper.like("title", title);
        }
        if (!StringUtils.isEmpty(pushDate)) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            wrapper.eq("push_date", format.format(pushDate));
        }
        wrapper.orderByDesc("push_date");
        this.page(page, wrapper);
        return page;
    }

    @Override
    public Map<String, Object> index() {
        HashMap<String, Object> res = new HashMap<>();
        //查询今日推送的课程
        List<Course> courses = baseMapper.getTodayPushCourses();
        res.put("courses", courses);
        //查询今日推送的班级
        List<Classroom> classrooms = baseMapper.getTodayPushClassrooms();
        res.put("classrooms", classrooms);
        return res;
    }

    @Transactional
    @Override
    public void deletePush(Integer[] ids) {
        //删除推送-课程关系表
        pushCourseService.remove(new UpdateWrapper<PushCourse>().in("push_id", ids));
        //删除推送-班级关系表
        pushClassService.remove(new UpdateWrapper<PushClass>().in("push_id", ids));
        //删除推送计划
        this.removeBatchByIds(Arrays.asList(ids));
    }

    @Override
    public Map<String, Object> getPushById(Integer id, boolean isContainCourse, boolean isContainClass) {
        PushSchedule push = getById(id);
        HashMap<String, Object> map = new HashMap<>();
        map.put("push", push);
        if (isContainCourse) {
            List<Course> courses = baseMapper.getPushCourse(id);
            map.put("courses", courses);
        }
        if (isContainClass) {
            List<Classroom> classrooms = baseMapper.getPushClass(id);
            map.put("classrooms", classrooms);
        }
        return map;
    }
}
